local global = _G.global
local Msg = require("core.Msg")
local Log = require("common.Log")
local Filter = require("middle.Filter")
local Config = require("Config")
local Broadcast = require("broadcast.Broadcast") 
local Lang = require("common.Lang")

MONITOR_ENABLE = true

MONITOR_PACKET = MONITOR_PACKET or {}	-- 用户包相关
MONITOR_TIMER = MONITOR_ADMIN or {}		-- 定时器相关
MONITOR_CUSTOM = MONITOR_CUSTOM or {}	-- 自定制

local handleTime1 = nil
local handleTime2 = nil

-- LL处理函数会用着human,srclogic
function handlePacket(input, container, protoId, human, srclogic)

  if Config.IS_MIDDLE then -- 跨服功能受限于黑名单配置
    if Filter.isBlack(protoId) then
      if protoId < 2048 and type(input) == "table"  then
        Broadcast.sendErr(input, Lang.MIDDLE_CG_NOT_IN_WHITE)
      end
      return
    end
  end
	if MONITOR_ENABLE then
		MONITOR_PACKET[0] = MONITOR_PACKET[0] or {0, 0}
		MONITOR_PACKET[protoId] = MONITOR_PACKET[protoId] or {0, 0}
		handleTime1 = global.get_usec()
	end
	
	Msg.proto_handler[protoId](input, container, human,srclogic)
	
	if MONITOR_ENABLE then
		handleTime2 = global.get_usec()
		MONITOR_PACKET[0][1] = MONITOR_PACKET[0][1] + 1							-- 包量
		MONITOR_PACKET[0][2] = MONITOR_PACKET[0][2] + handleTime2 - handleTime1 -- 处理时间
		
		MONITOR_PACKET[protoId][1] = MONITOR_PACKET[protoId][1] + 1
		MONITOR_PACKET[protoId][2] = MONITOR_PACKET[protoId][2] + handleTime2 - handleTime1
	end	
end

gdb_time = _G.gdb.gdb_time
function handleTimer(func, t, key)
	if MONITOR_ENABLE then
		handleTime1 = global.get_usec()
	end
  
  gdb_time(key)
  
	func(t)
	
	if MONITOR_ENABLE then
		MONITOR_TIMER[0] = MONITOR_TIMER[0] or {0, 0}
		MONITOR_TIMER[key] = MONITOR_TIMER[key] or {0, 0}
		
		handleTime2 = global.get_usec()		
		
		MONITOR_TIMER[0][1] = MONITOR_TIMER[0][1] + 1
		MONITOR_TIMER[0][2] = MONITOR_TIMER[0][2] + handleTime2 - handleTime1
		
		
		MONITOR_TIMER[key][1] = MONITOR_TIMER[key][1] + 1
		MONITOR_TIMER[key][2] = MONITOR_TIMER[key][2] + handleTime2 - handleTime1
	end
end

custom_table_temp = custom_table_temp or {}
function custom1(key)
	if not MONITOR_ENABLE then return end
	custom_table_temp[key] = global.get_usec()
end

function custom2(key)
	if not MONITOR_ENABLE then return end
	
	MONITOR_CUSTOM[key] = MONITOR_CUSTOM[key] or {0, 0}
	local time2 = global.get_usec()
	
	MONITOR_CUSTOM[key][1] = MONITOR_CUSTOM[key][1] + 1
	MONITOR_CUSTOM[key][2] = MONITOR_CUSTOM[key][2] + time2 - custom_table_temp[key]
end

local function cmp(a, b)
	return a[3] > b[3]
end

local function getThreadDesc()
  if _G.is_world then
     return "world:"
  else
     return "logic-".._G.logic_id..":"
  end
end
function output()
	if not MONITOR_ENABLE then return end
--	MONITOR_PACKET = {}
	
	local tempTable = {}
	local tempCount = 0
	for k, v in pairs(MONITOR_TIMER) do
		tempCount = tempCount + 1
		tempTable[tempCount] = {k, v[1], v[2]}
	end
	table.sort(tempTable, cmp)
	MONITOR_TIMER = {}
	
	local str = "TIMER:"
	str = str .. getThreadDesc()
	for i = 1, tempCount do
		if i > 7 then break end
		str = str .. " key:" .. tempTable[i][1] .. " c:" .. tempTable[i][2] .. " t:" .. tempTable[i][3]
	end
	Log.write(Log.LOGID_MONITOR, str)
	
	tempTable = {}
	tempCount = 0
	for k, v in pairs(MONITOR_PACKET) do
	   tempCount = tempCount + 1
	   tempTable[tempCount] = {k,v[1],v[2]}
	end
	table.sort(tempTable, cmp)
	MONITOR_PACKET = {}
	str = "IO:"..getThreadDesc()
	for i = 1, tempCount do
	   if i > 10 then break end
	   str = str .. " key:" .. tempTable[i][1] .. " c:" .. tempTable[i][2] .. " t:" .. tempTable[i][3]
	end
	Log.write(Log.LOGID_MONITOR, str)
	
	local tempTable = {}
	local tempCount = 0
	for k, v in pairs(MONITOR_CUSTOM) do
		tempCount = tempCount + 1
		tempTable[tempCount] = {k, v[1], v[2]}
	end
	table.sort(tempTable, cmp)
	if tempCount > 0 then
		local str = "CUSTOM :"..getThreadDesc()
		for i = 1, tempCount do
			str = str .. " key:" .. tempTable[i][1] .. " c:" .. tempTable[i][2] .. " t:" .. tempTable[i][3]
		end
		Log.write(Log.LOGID_MONITOR, str)	
		
		MONITOR_CUSTOM = {}
	end
end
